package com.mingmen.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.mingmen.supplier.R;
import com.mingmen.supplier.defineview.GlideRoundTransform;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.model.ShopManageModel;

import java.util.ArrayList;
import java.util.List;


public class CommodityShopAdapter extends RecyclerView.Adapter<CommodityShopAdapter.ViewHolder> {
    List<ShopManageModel.DataBean> myLiveList = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;

    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<ShopManageModel.DataBean> myLiveList);
    }
//小于50红色 大于50绿色

    public CommodityShopAdapter(Context context, List<ShopManageModel.DataBean> myLiveList) {
        this.mContext = context;
        this.myLiveList = myLiveList;

    }

    public void setDataList(List<ShopManageModel.DataBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_commodity_shop, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        ShopManageModel.DataBean dataBean = myLiveList.get(position);

        holder.commodityName.setText(dataBean.getCommodityName());
        holder.description.setText(dataBean.getDescription());
        holder.mothSales.setText("月售: " + dataBean.getMothSales());
        holder.spec.setText("规格: " + dataBean.getSpec());
        holder.price.setText("￥" + dataBean.getPrice() + "");
        if (dataBean.getInventory() < 50) {
            holder.inventory.setText("仅剩" + dataBean.getInventory() + "份");
            holder.inventory.setBackground(mContext.getResources().getDrawable(R.drawable.shape_rednormal_bg));
        } else if (dataBean.getInventory() == 0) {
            holder.inventory.setText("无货");
            holder.inventory.setBackground(mContext.getResources().getDrawable(R.drawable.shape_rednormal_bg));
        } else {
            holder.inventory.setText(dataBean.getInventory() + "份");
            holder.inventory.setBackground(mContext.getResources().getDrawable(R.drawable.shape_blue_normal_bg));
        }


        if (dataBean.getState().equals("2")) {
            holder.state.setText("已上架");
            holder.state.setTextColor(Color.parseColor("#55B767"));
        } else if (dataBean.getState().equals("3")) {
        /*    holder.state.setText("已下架");
            holder.state.setTextColor(Color.parseColor("#55B767"));*/
            holder.state.setText("未上架");
            holder.state.setTextColor(Color.parseColor("#929292"));
        } else {
            holder.state.setText("未上架");
            holder.state.setTextColor(Color.parseColor("#929292"));
        }

        Glide.with(mContext).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.default_pic).error(R.mipmap.default_pic).transform(new GlideRoundTransform(mContext, 5)).into(holder.img_pic);

//        Glide.with(mContext).load(AppConfig.IMGURL +dataBean.getPicture()).apply(mRequestOptions).into(mUserIcon);

//        Glide.with(mContext).load(AppConfig.IMGURL +dataBean.getPicture()).dontAnimate().placeholder(placePicRes).error(errorPicRes) .transform(new MultiTransformation(new CenterCrop(LoveBabyApplication.appContext),newGlideRoundTransform(LoveBabyApplication.appContext,10))).into(view);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.text_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout content;
        TextView text_manager;
        TextView commodityName;
        TextView description;
        TextView mothSales;

        TextView spec;
        TextView price;
        TextView state;
        TextView inventory;
        ImageView img_pic;

        public ViewHolder(View itemView) {
            super(itemView);

            content = itemView.findViewById(R.id.content);
            text_manager = itemView.findViewById(R.id.text_manager);
            commodityName = itemView.findViewById(R.id.commodityName);
            description = itemView.findViewById(R.id.description);
            mothSales = itemView.findViewById(R.id.mothSales);
            spec = itemView.findViewById(R.id.spec);
            price = itemView.findViewById(R.id.price);
            state = itemView.findViewById(R.id.state);
            inventory = itemView.findViewById(R.id.inventory);
            img_pic = itemView.findViewById(R.id.img_pic);
        }
    }


}
